package dk.lector.utils;


import dk.lector.datasource.ConfigDBRepository;
import dk.lector.exception.XMLException;
import dk.lector.providers.CONFIGContentProvider;
import android.content.Context;

public class URLBuilder {
	
	private static final String WebMethod_Login = "GetUserInfo";
	private static String WebMethod_GetConfig = "GetConfig";
	private static String WebMethod_GetDocumentByID = "GetDocumentByID";
	private static String WebMethod_GetDocumentInfo = "GetDocumentInfo";
	private static String WebMethod_SearchDocuments = "SearchDocuments";
	private static String WebMethod_GetFavorites = "GetFavorites";
	
	private static String server = "";
	private static String protocol ="";
	
	private static String prepareURL(Context context) throws XMLException {
	if (server != "" && protocol != "")
		return server;
	else
	{
		protocol = (ConfigDBRepository.getValue(context, "ssl").equalsIgnoreCase("ssl"))? "https://" : "http://";
		server = protocol + ConfigDBRepository.getValue(context, CONFIGContentProvider.SERVER_URL);
	}
	return server;
}
	
	public static String getLoginURL(Context context, String username,
			String password) {
		return prepareURL(context) + WebMethod_Login + "?username=" + username + "&password=" + password;
	}

	public static String getConfigURL(String url) {
	
		 return "http://" + server + "/TSMobileService.svc/"+ WebMethod_GetConfig;
	}

	public static String getDocumentByIdURL(Context context, int documentID,
			String username, String password) {
		return prepareURL(context) + WebMethod_GetDocumentByID + "?username=" + username + "&password=" + password
		+ "&id=" + documentID;
	}

	public static String getDocumentInfoURL(Context context, int documentID,
			String username, String password) {
		return prepareURL(context) + WebMethod_GetDocumentInfo 
				+ "?username=" + username + "&password=" + password
				+ "&id=" + documentID;
	}

	public static String searchDocumentsURL(Context context,
			String documentID, String name, String username,
			String password) {
		
		return prepareURL(context) + WebMethod_SearchDocuments + "?username=" + username + "&password=" + password
				+ "&id=" + documentID + "&name=" + name;
	}

	public static String getFavoritesURL(Context appContext, String username,
			String password) {
		return prepareURL(appContext) + WebMethod_GetFavorites + "?username=" + username + "&password=" + password;
	}

}

